package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Department;
import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        String username = (String) para.get("username");
        String password = (String) para.get("password");
        User user = userService.Login(username, password);
        HashMap<String, Object> hs = new HashMap<>();
        String token = null;
        if (user != null) {
            token = TokenUtil.sign(user);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 403));
        }
        hs.put("token", token);
        hs.put("data", user);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "searchUser",method = RequestMethod.POST)
    public String searchDept(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        //获取参数
        ObjectMapper objectMapper = new ObjectMapper();
        Object heightQuery = para.get("heightQuery");
        User user = objectMapper.convertValue(heightQuery,User.class);
        Integer pageNum = Integer.parseInt(String.valueOf(para.get("pageNum")));
        Integer pageSize = Integer.parseInt(String.valueOf(para.get("pageSize")));
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
       List<User> userList = userService.heightQueryUser(user);
        PageInfo<User> info = new PageInfo<>(userList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("users", userList);
        if (userList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取到"+info.getTotal()+"条数据", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }
        return objectMapper.writeValueAsString(hs);
    }


    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String test(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data", "data");
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/findUserByPage", method = RequestMethod.GET)
    public String findUserByPage(@RequestParam Map<String, String> para) throws JsonProcessingException {
        //获取参数
        String query = para.get("query");
        Integer pageNum = Integer.parseInt(para.get("pageNum"));
        Integer pageSize = Integer.parseInt(para.get("pageSize"));
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userService.queryAllUser(query);
        PageInfo<User> info = new PageInfo<>(userList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("users", userList);
        if (userList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取成功", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/modifyUser", method = RequestMethod.PUT)
    public String modifyUser(@RequestBody User user) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(userService.modifyUser(user)){
             hs.put("meta", new Constance("修改成功", 200));
         }else{
            hs.put("meta", new Constance("修改失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteUserById", method = RequestMethod.DELETE)
    public String deleteUser(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(userService.removeUserById(id)){
            hs.put("meta", new Constance("删除成功", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteUsersById", method = RequestMethod.POST)
    public String deleteUsers(@RequestBody Map<String,List<Integer>>ids) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Integer num = userService.removeUsers(ids.get("ids"));
        if(num>0){
            hs.put("meta", new Constance("删除成功！"+num+"条数据被删除", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public String addUser(@RequestBody User user)throws JsonProcessingException{
        HashMap<String, Object> hs = new HashMap<>();
        if(userService.addUser(user)){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value="/findUserById",method = RequestMethod.GET)
    public String findUserById(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        User user = userService.queryUserById(id);
        if(user!=null){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        hs.put("data", user);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}